let fs = require('fs')
let data = fs.readFileSync('1.txt','utf-8')

let readfile = () =>{
    return new Promise((resolve, reject) => {
        try{
            fs.readFile('1.txt','utf-8',function (err,data) {
                console.log('读取文件data：',data)
                resolve(data)
            })
        }catch (e) {
                reject(e)
        }
    })
}

let writefile = (data) =>{
    return new Promise((resolve, reject) => {
        fs.writeFile('./2.txt',data,"utf-8",function (err) {
            if (err){
                reject(err)
            }
            resolve('写入成功')
        })
    })
}

let statfile = () =>{
    return new Promise((resolve, reject) => {
        fs.statSync('./2.txt',function (err,stat) {
            if (err){
                console.log('读取失败！')
                reject(err)
            }
            resolve(stat)
        })

    })
}

// 如果想使用async，await，promise。
// 调用函数的外面修饰为async
// promise函数前面加上await

let checkfile = async () =>{
    try{
    let data = await readfile()
    let res = await writefile(data)
        console.log('res:',res)
    let stat = await statfile()
        console.log('stat:',stat)
    }catch(e)
    {
        console.log('e:',e)
    }
}
checkfile()